package com.sqtracker.recommender.controller;

import com.sqtracker.recommender.model.Rating;
import com.sqtracker.recommender.service.RatingService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/ratings")
public class RatingController {
    private static final Logger logger = LoggerFactory.getLogger(RatingController.class);

    private final RatingService ratingService;

    @Autowired
    public RatingController(RatingService ratingService) {
        this.ratingService = ratingService;
    }

    @PostMapping("/{torrentId}")
    public ResponseEntity<Rating> createOrUpdateRating(
            @PathVariable String torrentId,
            @RequestAttribute String userId,
            @RequestBody Map<String, Object> payload) {
        try {
            int score = (int) payload.get("score");
            String comment = (String) payload.get("comment");

            if (score < 1 || score > 5) {
                return ResponseEntity.badRequest().build();
            }

            Rating rating = ratingService.createOrUpdateRating(userId, torrentId, score, comment);
            return ResponseEntity.ok(rating);
        } catch (Exception e) {
            logger.error("Error creating/updating rating", e);
            return ResponseEntity.badRequest().build();
        }
    }

    @GetMapping("/torrent/{torrentId}")
    public ResponseEntity<List<Rating>> getTorrentRatings(@PathVariable String torrentId) {
        try {
            List<Rating> ratings = ratingService.getTorrentRatings(torrentId);
            return ResponseEntity.ok(ratings);
        } catch (Exception e) {
            logger.error("Error getting torrent ratings", e);
            return ResponseEntity.badRequest().build();
        }
    }

    @GetMapping("/user/{userId}")
    public ResponseEntity<List<Rating>> getUserRatings(@PathVariable String userId) {
        try {
            List<Rating> ratings = ratingService.getUserRatings(userId);
            return ResponseEntity.ok(ratings);
        } catch (Exception e) {
            logger.error("Error getting user ratings", e);
            return ResponseEntity.badRequest().build();
        }
    }

    @GetMapping("/torrent/{torrentId}/user/{userId}")
    public ResponseEntity<Rating> getUserRatingForTorrent(
            @PathVariable String torrentId,
            @PathVariable String userId) {
        try {
            return ratingService.getUserRatingForTorrent(userId, torrentId)
                    .map(ResponseEntity::ok)
                    .orElse(ResponseEntity.notFound().build());
        } catch (Exception e) {
            logger.error("Error getting user rating for torrent", e);
            return ResponseEntity.badRequest().build();
        }
    }

    @GetMapping("/torrent/{torrentId}/stats")
    public ResponseEntity<Map<String, Object>> getTorrentRatingStats(@PathVariable String torrentId) {
        try {
            double averageRating = ratingService.getAverageRating(torrentId);
            long ratingCount = ratingService.getRatingCount(torrentId);

            Map<String, Object> stats = Map.of(
                "averageRating", averageRating,
                "ratingCount", ratingCount
            );

            return ResponseEntity.ok(stats);
        } catch (Exception e) {
            logger.error("Error getting torrent rating stats", e);
            return ResponseEntity.badRequest().build();
        }
    }

    @DeleteMapping("/{ratingId}")
    public ResponseEntity<Void> deleteRating(
            @PathVariable String ratingId,
            @RequestAttribute String userId) {
        try {
            ratingService.deleteRating(ratingId);
            return ResponseEntity.ok().build();
        } catch (Exception e) {
            logger.error("Error deleting rating", e);
            return ResponseEntity.badRequest().build();
        }
    }
} 